package org.easyx.ser.sys.serivce.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.easyx.ser.sys.entity.SysRole;
import org.easyx.ser.sys.entity.SysRoleResource;
import org.easyx.ser.sys.mapper.SysRoleMapper;
import org.easyx.ser.sys.serivce.SysRoleResourceService;
import org.easyx.ser.sys.serivce.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author xiaojl
 * @since 2017-09-11
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

	@Autowired
	private SysRoleMapper sysRoleMapper;
	@Autowired
	private SysRoleResourceService sysRoleResourceService;
	
	@Override
	public void updateRoleResource(Long id, String resourceIds) {
		
		sysRoleResourceService.delete(new EntityWrapper<SysRoleResource>().eq("roleId",id));
		
		String[] rids = resourceIds.split(",");
		if(rids!=null && rids.length>0){
			List<SysRoleResource> rrlist = new ArrayList<SysRoleResource>();
			for(String s: rids){
				SysRoleResource r = new SysRoleResource();
				r.setResourceId(Long.parseLong(s));
				r.setRoleId(id);
				r.setCreateTime(new Date());
				r.setDelFlag(0);
				rrlist.add(r);
			}
			sysRoleResourceService.insertBatch(rrlist);
		}
	}

	@Override
	public List<SysRole> selectListByUserId(Long userId) {
		return sysRoleMapper.selectListByUserId(userId);
	}
	
}
